function openFile(fileURL){
	window.plugins.fileOpener.open(fileURL);
}

function downloadFile(fileToDownload){
	var divEl = document.getElementById("ready");
	divEl.innerHTML = 'Descargando planilla...';
	$.mobile.loadingMessage = "Procesando...";
 	$.mobile.loading('show');
    window.requestFileSystem(
            LocalFileSystem.PERSISTENT, 0, 
            function onFileSystemSuccess(fileSystem) {
            
            	//alert("about to download");
            	fileSystem.root.getFile(
                        "dummy.html", {create: true, exclusive: false}, 
                        function gotFileEntry(fileEntry){
                            //alert("about to download 2");
                            var sPath = fileEntry.fullPath.replace("dummy.html","");
                            var fileTransfer = new FileTransfer();
                            fileEntry.remove();

                            fileTransfer.download(
                                      "http://www.xeliom.com/TD/planillas/" + fileToDownload,
                                      sPath + fileToDownload,
                                      function(theFile) {
                                    	  //alert("download complete: " + theFile.toURI());
                                          divEl.innerHTML = "Descarga completa: " + theFile.toURI();
                                          $.mobile.loading('hide');
                                          openFile(theFile.toURI());
                                      },
                                      function(error) {
                          			 	  $.mobile.loading('hide');
                                          divEl.innerHTML = 'Error en la descarga: ' + error.source + ' - ' + error.target + ' - ' + error.code;
                                      }
                                      ); 
                        }, 
                        fail);
            }, 
            fail);

}

function fail(error) {
	alert(error.code);
}

function sendMail(fileID){
	window.location.href = '../sendEmail.html?id=' + fileID;
	//$.mobile.changePage('../sendEmail.html', { dataUrl : "sendEmail.html?id=" + fileID, data : { 'id' : fileID }, reloadPage : true, changeHash : true });
}